<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- <form action="" enctype="application/x-www-form-urlencoded"></form> -->
    用户名:<input type="text" id="user" /><span id="userTip"></span>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    <script>
      $("#user").on("blur", function () {
        const value = $(this).val().trim();
        if (!value) {
          $("#userTip").text("请输入用户名").css("color", "red");
          return;
        }
        $.ajax({
          url: "http://localhost:3000/validatorUsername",
          dataType: "json",
          type:'POST',
          data: {
            username: value,
          },
          beforeSend: function () {
            console.log("响应之前");
          },
          success: function (data) {
            console.log(data);
            data.code === 20000
              ? $("#userTip").text(data.message).css("color", "green")
              : $("#userTip").text(data.message).css("color", "red");
            // $('#userTip').text(data.message).css('color','green')
          },
          error: function (xhr) {
            console.log(xhr, "失败了");
          },
          complete: function () {
            console.log("响应完成");
          },
        });
      });
    </script>
  </body>
</html>
